package com.tjetc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.tjetc.constant.CommonConstant;
import com.tjetc.dao.GuestDao;
import com.tjetc.dao.OrderDao;
import com.tjetc.dao.RoomDao;
import com.tjetc.dao.impl.GuestDaoImpl;
import com.tjetc.dao.impl.OrderDaoImpl;
import com.tjetc.dao.impl.RoomDaoImpl;
import com.tjetc.exception.MessageException;
import com.tjetc.pojo.dto.GuestDTO;
import com.tjetc.pojo.dto.OrderDTO;
import com.tjetc.pojo.entity.Guest;
import com.tjetc.pojo.entity.Order;
import com.tjetc.pojo.entity.Room;
import com.tjetc.service.OrderService;
import com.tjetc.utils.DruidUtils;

import java.util.List;

/**
 * @Author: itda
 * @Date: 2024/5/11 14:01
 * @Description: OrderServiceImpl
 */
public class OrderServiceImpl implements OrderService {

    private RoomDao roomDao = new RoomDaoImpl();

    private OrderDao orderDao = new OrderDaoImpl();

    private GuestDao guestDao = new GuestDaoImpl();

    @Override
    public void save(OrderDTO orderDTO) {
        try {
            Order order = new Order();
            BeanUtil.copyProperties(orderDTO, order);
            GuestDTO guestDTO = guestDao.getById(order.getGuestId());
            if (guestDTO == null){
                throw new MessageException(CommonConstant.NO_GUEST_INFO);
            }
            Room room = roomDao.getById(order.getRoomId());
            if (room == null) {
                throw new MessageException(CommonConstant.NO_FIND_ROOM);
            }
            if (room.getAvailability() > 1) {
                throw new MessageException(CommonConstant.ROOM_IS_BOOKED);
            }
            order.setTotalPrice(room.getPrice());
            DruidUtils.start();
            orderDao.insert(order);
            //修改房间状态
            roomDao.updateStatusById(order.getRoomId(),room.getAvailability());
            DruidUtils.commit();
            System.out.println(CommonConstant.ADD_SUCCESS);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            DruidUtils.rollback();
        }
    }

    @Override
    public void delete(Long id) {
        try {
            DruidUtils.start();
            orderDao.delete(id);
            DruidUtils.commit();
            System.out.println(CommonConstant.DELETE_SUCCESS);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            DruidUtils.rollback();
        }

    }

    @Override
    public void update(OrderDTO orderDTO) {
        try {
            DruidUtils.start();
            Order order = new Order();
            BeanUtil.copyProperties(orderDTO, order);
            orderDao.update(order);
            DruidUtils.commit();
            System.out.println(CommonConstant.UPDATE_SUCCESS);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            DruidUtils.rollback();
        }
    }

    @Override
    public void list() throws Exception{
        List<Order> list = orderDao.list();
        if (list == null || list.size() == 0) {
            throw new MessageException(CommonConstant.NO_FIND_DATA);
        }
        list.forEach(System.out::println);
    }
}
